let express = require('express');
let router = express.Router();
let allsDao = require("../dao/AllsDao");
let shoppingAndStarDao = require("../dao/ShoppingAndStarDao");

let pager = require("../util/PagerHelper")

/* GET users listing. */
router.all('/goods/list.do', function(req, res, next) {
 pager.href ="/api/goods/list.do";
    let where = "";
    let and =""
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (where.trim().length === 0) {
                where += " where ";
            }
            where += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("where:"+where);
    }
    allsDao.getCount(where,and).then(count => {
    pager.setCount(count);
    return allsDao.findByPage(where,and);
  }).then(data => {
      pager.data = data;
    res.json({
      code:200, //结果编码,200表示成功，404表示未找到
      data:pager
    });
  }).catch(err => next(err));
});

router.get("/goods/edit.do", (req, res, next) => {
    let id = req.query.id;
    console.log("id:"+id);
    if (id) {
        allsDao.findById(id).then(editObj => {
            if (editObj) {
                res.json({
                    code: 200,
                    data: editObj
                });
            } else {
                res.json({
                    code: 404,
                    msg: "没有找到记录！"
                });
            }
        }).catch(err => next(err));
    } else {
        res.json({
            code: 410,
            msg: "没有id！"
        });
    }
});

/*
获取单条数据
 */
router.get("/goods/getone.do", (req, res, next) => {
    let id = req.query.id;
    allsDao.findById(id).then(data => {
        console.log(data)
            res.json({
                code: 200,
                data
            })
        }).catch(err => next(err));
});


router.post("/goods/buy.do", (req, res, next) => {
        let newObj = Object.assign({}, req.body);
        let table = "shoppingcard";
        //添加
        newObj.id = 0;//id是自动增长，必须是0值
        shoppingAndStarDao.insert(table,newObj).then(data => res.json({
            code: 200,
            msg: "添加购物车成功，再去逛逛吧"
        })).catch(err => next(err));
});

router.post("/goods/star.do", (req, res, next) => {
    let newObj = Object.assign({}, req.body);
    let table = "star";
    //添加
    newObj.id = 0;//id是自动增长，必须是0值
    shoppingAndStarDao.insert(table,newObj).then(data => res.json({
        code: 200,
        msg: "收藏成功，再去逛逛吧"
    })).catch(err => next(err));

});


router.all("/bags/list.do",function (req,res,next) {
    pager.href = "/api/bags/list.do"
    let where ="where category = 1"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})

router.all("/pants/list.do",function (req,res,next) {
    pager.href = "/api/pants/list.do"
    let where ="where category = 3"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})

router.all("/dress/list.do",function (req,res,next) {
    pager.href = "/api/dress/list.do"
    let where ="where category = 2"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})
router.all("/shorts/list.do",function (req,res,next) {
    pager.href = "/api/shorts/list.do"
    let where ="where category = 4"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})

router.all("/skirts/list.do",function (req,res,next) {
    pager.href = "/api/skirts/list.do"
    let where ="where category = 5"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})
router.all("/sweaters/list.do",function (req,res,next) {
    pager.href = "/api/sweaters/list.do"
    let where ="where category = 6"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})
router.all("/tshirts/list.do",function (req,res,next) {
    pager.href = "/api/tshirts/list.do"
    let where ="where category = 7"
    let and = "";
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.goodname) {
            if (and.trim().length === 0) {
                and += " and ";
            }
            and += ` goodname like '%${req.body.goodname}%' `;
        }
        console.log("and:"+and);
    }
    allsDao.getCount(where,and).then(count => {
        pager.setCount(count);
        return allsDao.findByPage(where,and);
    }).then(data => {
        pager.data = data
        res.json({
                code: 200,
                data : pager
            }
        )
    }).catch(err => next(err));
})

module.exports = router;
